package com.dream.libxrec.encode.audio;

import android.content.Context;
import android.os.Bundle;

import com.dream.libxrec.utils.G711aUtil;

public class G711AEncoder extends IAudioEncoder{
    private boolean isRun = false;
    private byte[] g711aBuffer = null;

    @Override
    public IAudioEncoder init(Context ct, int sampleRate,
                              int codecBitRate, int channelNum, Bundle extra) {
        //G711A编码不需要初始化,编码过程就是1:2的压缩，和采样率等参数无关
        return this;
    }

    @Override
    public void encode(byte[] pcm, int offset, int size) {
        encode(pcm,offset,size,0);
    }

    @Override
    public void encode(byte[] pcm, int offset, int size,
                       long presentationTimeUs) {
        int count = size/2;
        if (g711aBuffer == null||g711aBuffer.length < count) {
            g711aBuffer = new byte[count];
        }

        if (mCb!= null) {
            G711aUtil.G711aEncoder(pcm, g711aBuffer, size,
                    false);
            mCb.onFrame(g711aBuffer, count,presentationTimeUs);
        }
    }

    @Override
    public void start() { isRun = true;}

    @Override
    public boolean isRun() {
        return isRun;
    }

    @Override
    public void stop() {
        isRun = false;
        g711aBuffer = null;
    }
}
